#include "main.h"
#include "notice.h"
#include "weather_an.h"
#include "time.h"
#include "button.h"



// 主界面及其子界面的详情图功能
#include "main.h"

// 主界面
static lv_obj_t * main_win = NULL;
// 背景图片相关
static lv_obj_t * background_img = NULL;
static int current_background_index = 0;
static const char * background_images[] = {
    "S:/IOT/bling/pt1.png",
    "S:/IOT/bling/pt2.png",
    "S:/IOT/bling/pt3.png"
};

// 子界面相关
static lv_obj_t * xj1_win = NULL;
static lv_obj_t * xj2_win = NULL;
static lv_obj_t * xj3_win = NULL;

// 图标按钮
lv_obj_t * lv_imgbtn(lv_obj_t * parent, int x, int y, int w, int h, char * pic_path);

// 背景图片点击事件回调函数
static void background_img_event_callback(lv_event_t * event);

// 展示细节按钮的回调函数
static void detail_btn_event_callback(lv_event_t * event);

// 子界面返回事件函数
static void xj_return(lv_event_t * e, lv_obj_t * win);

// 显示子界面xj1
static void show_xj1_win();

// 显示子界面xj2
static void show_xj2_win();

// 显示子界面xj3
static void show_xj3_win();


// 显示主界面
void show_main_win()
{
   // 1.创建主界面
   main_win = lv_obj_create(lv_scr_act()); // 创建一个独立的对象，大小与屏幕一致

   // 2.设置主界面的大小与屏幕一致
   lv_obj_set_size(main_win, 800, 480);

   // 在主界面中创建时间图标(注意：要使用开发板里面的绝对路径)
   lv_obj_t * time = lv_imgbtn(main_win, 600, 5, 50, 50, "S:/IOT/bling/time.png");
   lv_obj_add_event_cb(time, time_btn_event_callback, LV_EVENT_CLICKED, NULL);

   // 在主界面中创建天气图标(注意：要使用开发板里面的绝对路径)
   lv_obj_t * weather = lv_imgbtn(main_win, 400, 5, 50, 50, "S:/IOT/bling/weather.png");
   lv_obj_add_event_cb(weather, weather_btn_event_callback, LV_EVENT_CLICKED, NULL);

   // 在主界面中创建公告图标(注意：要使用开发板里面的绝对路径)
   lv_obj_t * an = lv_imgbtn(main_win, 5, 5, 200, 100, "S:/IOT/bling/an.png");
   lv_obj_add_event_cb(an, an_btn_event_callback, LV_EVENT_CLICKED, NULL);

   // 在主界面中创建注意事项1图标(注意：要使用开发板里面的绝对路径)
   lv_obj_t * notice1 = lv_imgbtn(main_win, 5, 150, 200, 100, "S:/IOT/bling/notice1.png");
   lv_obj_add_event_cb(notice1, notice1_btn_event_callback, LV_EVENT_CLICKED, NULL);

  // 在主界面中创建注意事项2图标(注意：要使用开发板里面的绝对路径)
  lv_obj_t * notice2 = lv_imgbtn(main_win, 5, 300, 200, 100, "S:/IOT/bling/notice2.png");
  lv_obj_add_event_cb(notice2, notice2_btn_event_callback, LV_EVENT_CLICKED, NULL);

  // 在主界面中创建注意事项3图标(注意：要使用开发板里面的绝对路径)
  lv_obj_t * notice3 = lv_imgbtn(main_win, 5, 450, 200, 100, "S:/IOT/bling/notice3.png");
  lv_obj_add_event_cb(notice3, notice3_btn_event_callback, LV_EVENT_CLICKED, NULL);

   //背景图

// 创建背景图片
background_img = lv_img_create(main_win);
lv_img_set_src(background_img, background_images[current_background_index]);
lv_obj_align(background_img, LV_ALIGN_CENTER, 0, 0);
lv_obj_move_background(background_img); // 将背景图片移动到最底层
 // 在show_main_win函数中为背景图添加点击事件
 lv_obj_add_event_cb(background_img, background_img_event_callback, LV_EVENT_CLICKED, NULL);
// 创建上一张背景按钮
lv_obj_t * prev_bg_btn = lv_btn_create(main_win);
lv_obj_set_size(prev_bg_btn, 80, 40);
lv_obj_set_pos(prev_bg_btn, 200, 500); // 设置按钮位置为 (200, 500)
lv_obj_add_event_cb(prev_bg_btn, prev_bg_btn_event_callback, LV_EVENT_CLICKED, NULL);

// 创建上一张背景按钮的标签
lv_obj_t * prev_bg_label = lv_label_create(prev_bg_btn);
lv_obj_set_style_text_font(prev_bg_label, &lv_font_source_han_sans_bold_20, 0);
lv_label_set_text(prev_bg_label, "上一张");
lv_obj_center(prev_bg_label);

// 创建下一张背景按钮
lv_obj_t * next_bg_btn = lv_btn_create(main_win);
lv_obj_set_size(next_bg_btn, 80, 40);
lv_obj_set_pos(next_bg_btn, 700, 500); // 设置按钮位置为 (700, 500)
lv_obj_add_event_cb(next_bg_btn, next_bg_btn_event_callback, LV_EVENT_CLICKED, NULL);

// 创建下一张背景按钮的标签
lv_obj_t * next_bg_label = lv_label_create(next_bg_btn);
lv_obj_set_style_text_font(next_bg_label, &lv_font_source_han_sans_bold_20, 0);
lv_label_set_text(next_bg_label, "下一张");
lv_obj_center(next_bg_label);

     
////展示细节按钮
// 在主界面中创建展示细节按钮
lv_obj_t * detail_btn = lv_btn_create(main_win);
lv_obj_set_size(detail_btn, 80, 40);
lv_obj_set_pos(detail_btn, 800, 240); // 设置按钮位置为 (800, 240)

// 创建按钮标签
lv_obj_t * detail_label = lv_label_create(detail_btn);
lv_obj_set_style_text_font(detail_label, &chinese_ziku, 0);
lv_label_set_text(detail_label, "展示细节");
lv_obj_center(detail_label);

// 添加按钮的事件，当按钮被点击后就会触发 event_cb 函数
lv_obj_add_event_cb(detail_btn, detail_btn_event_callback, LV_EVENT_CLICKED, NULL);


}

// 上一张背景按钮的回调函数
static void prev_bg_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if (code == LV_EVENT_CLICKED) {
        current_background_index--;
        if (current_background_index < 0) {
            current_background_index = sizeof(background_images) / sizeof(background_images[0]) - 1;
        }
        lv_img_set_src(background_img, background_images[current_background_index]);
    }
}

// 下一张背景按钮的回调函数
static void next_bg_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if (code == LV_EVENT_CLICKED) {
        current_background_index++;
        if (current_background_index >= sizeof(background_images) / sizeof(background_images[0])) {
            current_background_index = 0;
        }
        lv_img_set_src(background_img, background_images[current_background_index]);
    }
}
//注意事项2
static void notice2_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口2的容器添加隐藏属性，清除窗口1的隐藏属性
        lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示天气界面
        show_notice2_win();
#endif
    }
}

//注意事项3
static void notice3_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口2的容器添加隐藏属性，清除窗口1的隐藏属性
        lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示天气界面
        show_notice3_win();
#endif
    }
}

//注意事项1
static void notice1_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口2的容器添加隐藏属性，清除窗口1的隐藏属性
        lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示天气界面
        show_notice1_win();
#endif
    }
}


//时间
static void time_btn_event_callback(lv_event_t * event)
{
    // 获取事件号
    lv_event_code_t code = lv_event_get_code(event);

    // 单击事件
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口1的容器添加隐藏属性，清除窗口2的隐藏属性
        lv_obj_add_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示时间界面
        show_time_win();
#endif
    }
}

//天气
static void weather_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口2的容器添加隐藏属性，清除窗口1的隐藏属性
        lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示天气界面
        show_weather_win();
#endif
    }
}

//公告
static void an_btn_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
#if HIDDEN_WIN
        // 给窗口2的容器添加隐藏属性，清除窗口1的隐藏属性
        lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN);
#else
        // 删除主界面
        lv_obj_del(main_win);

        // 显示天气界面
        show_an_win();
#endif
    }
}